找传奇、传世资源到传世资源站!

Luciano Ramalho

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

经典书籍 fluent pythonfrom clipboardTable of ContentsPreface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvPart I. Prologue1. The Python Data Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3A Pythonic Card Deck 4How Special Methods Are Used 8Emulating Numeric Types 9String Representation 11Arithmetic Operators 12Boolean Value of a Custom Type 12Overview of Special Methods 13Why len Is Not a Method 14Chapter Summary 14Further Reading 15Part II. Data Structures2. An Array of Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Overview of Built-In Sequences 20List Comprehensions and Generator Expressions 21List Comprehensions and Readability 21Listcomps Versus map and filter 23Cartesian Products 23Generator Expressions 25Tuples Are Not Just Immutable Lists 26Tuples as Records 26Tuple Unpacking 27vNested Tuple Unpacking 29Named Tuples 30Tuples as Immutable Lists 32Slicing 33Why Slices and Range Exclude the Last Item 33Slice Objects 34Multidimensional Slicing and Ellipsis 35Assigning to Slices 36Using and * with Sequences 36Building Lists of Lists 37Augmented Assignment with Sequences 38A = Assignment Puzzler 40list.sort and the sorted Built-In Function 42Managing Ordered Sequences with bisect 44Searching with bisect 44Inserting with bisect.insort 47When a List Is Not the Answer 48Arrays 48Memory Views 51NumPy and SciPy 52Deques and Other Queues 55Chapter Summary 57Further Reading 593. Dictionaries and Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Generic Mapping Types 64dict Comprehensions 66Overview of Common Mapping Methods 66Handling Missing Keys with setdefault 68Mappings with Flexible Key Lookup 70defaultdict: Another Take on Missing Keys 70The __missing__ Method 72Variations of dict 75Subclassing UserDict 76Immutable Mappings 77Set Theory 79set Literals 80Set Comprehensions 81Set Operations 82dict and set Under the Hood 85A Performance Experiment 85Hash Tables in Dictionaries 87vi | Table of ContentsPractical Consequences of How dict Works 90How Sets Work—Practical Consequences 93Chapter Summary 93Further Reading 944. Text versus Bytes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Character Issues 98Byte Essentials 99Structs and Memory Views 102Basic Encoders/Decoders 103Understanding Encode/Decode Problems 105Coping with UnicodeEncodeError 105Coping with UnicodeDecodeError 106SyntaxError When Loading Modules with Unexpected Encoding 108How to Discover the Encoding of a Byte Sequence 109BOM: A Useful Gremlin 110Handling Text Files 111Encoding Defaults: A Madhouse 114Normalizing Unicode for Saner Comparisons 117Case Folding 119Utility Functions for Normalized Text Matching 120Extreme “Normalization”: Taking Out Diacritics 121Sorting Unicode Text 124Sorting with the Unicode Collation Algorithm 126The Unicode Database 127Dual-Mode str and bytes APIs 129str Versus bytes in Regular Expressions 129str Versus bytes on os Functions 130Chapter Summary 132Further Reading 133Part III. Functions as Objects5. First-Class Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Treating a Function Like an Object 140Higher-Order Functions 141Modern Replacements for map, filter, and reduce 142Anonymous Functions 143The Seven Flavors of Callable Objects 144User-Defined Callable Types 145Function Introspection 146Table of Contents | viiFrom Positional to Keyword-Only Parameters 148Retrieving Information About Parameters 150Function Annotations 154Packages for Functional Programming 156The operator Module 156Freezing Arguments with functools.partial 159Chapter Summary 161Further Reading 1626. Design Patterns with First-Class Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Case Study: Refactoring Strategy 168Classic Strategy 168Function-Oriented Strategy 172Choosing the Best Strategy: Simple Approach 175Finding Strategies in a Module 176Command 177Chapter Summary 179Further Reading 1807. Function Decorators and Closures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Decorators 101 184When Python Executes Decorators 185Decorator-Enhanced Strategy Pattern 187Variable Scope Rules 189Closures 192The nonlocal Declaration 195Implementing a Simple Decorator 196How It Works 198Decorators in the Standard Library 199Memoization with functools.lru_cache 200Generic Functions with Single Dispatch 202Stacked Decorators 205Parameterized Decorators 206A Parameterized Registration Decorator 206The Parameterized Clock Decorator 209Chapter Summary 211Further Reading 212viii | Table of ContentsPart IV. Object-Oriented Idioms8. Object References, Mutability, and Recycling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Variables Are Not Boxes 220Identity, Equality, and Aliases 221Choosing Between == and is 223The Relative Immutability of Tuples 224Copies Are Shallow by Default 225Deep and Shallow Copies of Arbitrary Objects 228Function Parameters as References 229Mutable Types as Parameter Defaults: Bad Idea 230Defensive Programming with Mutable Parameters 232del and Garbage Collection 234Weak References 236The WeakValueDictionary Skit 237Limitations of Weak References 239Tricks Python Plays with Immutables 240Chapter Summary 242Further Reading 2439. A Pythonic Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Object Representations 248Vector Class Redux 248An Alternative Constructor 251classmethod Versus staticmethod 252Formatted Displays 253A Hashable Vector2d 257Private and “Protected” Attributes in Python 262Saving Space with the __slots__ Class Attribute 264The Problems with __slots__ 267Overriding Class Attributes 267Chapter Summary 269Further Reading 27110. Sequence Hacking, Hashing, and Slicing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Vector: A User-Defined Sequence Type 276Vector Take #1: Vector2d Compatible 276Protocols and Duck Typing 279Vector Take #2: A Sliceable Sequence 280How Slicing Works 281A Slice-Aware __getitem__ 283Vector Take #3: Dynamic Attribute Access 284Table of Contents | ixVector Take #4: Hashing and a Faster == 288Vector Take #5: Formatting 294Chapter Summary 301Further Reading 30211. Interfaces: From Protocols to ABCs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Interfaces and Protocols in Python Culture 308Python Digs Sequences 310Monkey-Patching to Implement a Protocol at Runtime 312Alex Martelli’s Waterfowl 314Subclassing an ABC 319ABCs in the Standard Library 321ABCs in collections.abc 321The Numbers Tower of ABCs 323Defining and Using an ABC 324ABC Syntax Details 328Subclassing the Tombola ABC 329A Virtual Subclass of Tombola 332How the Tombola Subclasses Were Tested 335Usage of register in Practice 338Geese Can Behave as Ducks 338Chapter Summary 340Further Reading 34212. Inheritance: For Good or For Worse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347Subclassing Built-In Types Is Tricky 348Multiple Inheritance and Method Resolution Order 351Multiple Inheritance in the Real World 356Coping with Multiple Inheritance 3581. Distinguish Interface Inheritance from Implementation Inheritance 3592. Make Interfaces Explicit with ABCs 3593. Use Mixins for Code Reuse 3594. Make Mixins Explicit by Naming 3595. An ABC May Also Be a Mixin; The Reverse Is Not True 3606. Don’t Subclass from More Than One Concrete Class 3607. Provide Aggregate Classes to Users 3608. “Favor Object Composition Over Class Inheritance.” 361Tkinter: The Good, the Bad, and the Ugly 361A Modern Example: Mixins in Django Generic Views 362Chapter Summary 366Further Reading 367x | Table of Contents13. Operator Overloading: Doing It Right. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Operator Overloading 101 372Unary Operators 372Overloading for Vector Addition 375Overloading * for Scalar Multiplication 380Rich Comparison Operators 384Augmented Assignment Operators 388Chapter Summary 392Further Reading 393Part V. Control Flow14. Iterables, Iterators, and Generators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Sentence Take #1: A Sequence of Words 402Why Sequences Are Iterable: The iter Function 404Iterables Versus Iterators 405Sentence Take #2: A Classic Iterator 409Making Sentence an Iterator: Bad Idea 411Sentence Take #3: A Generator Function 412How a Generator Function Works 413Sentence Take #4: A Lazy Implementation 416Sentence Take #5: A Generator Expression 417Generator Expressions: When to Use Them 419Another Example: Arithmetic Progression Generator 420Arithmetic Progression with itertools 423Generator Functions in the Standard Library 424New Syntax in Python 3.3: yield from 433Iterable Reducing Functions 434A Closer Look at the iter Function 436Case Study: Generators in a Database Conversion Utility 437Generators as Coroutines 439Chapter Summary 439Further Reading 44015. Context Managers and else Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447Do This, Then That: else Blocks Beyond if 448Context Managers and with Blocks 450The contextlib Utilities 454Using @contextmanager 455Chapter Summary 459Further Reading 459Table of Contents | xi16. Coroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463How Coroutines Evolved from Generators 464Basic Behavior of a Generator Used as a Coroutine 465Example: Coroutine to Compute a Running Average 468Decorators for Coroutine Priming 469Coroutine Termination and Exception Handling 471Returning a Value from a Coroutine 475Using yield from 477The Meaning of yield from 483Use Case: Coroutines for Discrete Event Simulation 489About Discrete Event Simulations 489The Taxi Fleet Simulation 490Chapter Summary 498Further Reading 50017. Concurrency with Futures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505Example: Web Downloads in Three Styles 505A Sequential Download Script 507Downloading with concurrent.futures 509Where Are the Futures? 511Blocking I/O and the GIL 515Launching Processes with concurrent.futures 515Experimenting with Executor.map 517Downloads with Progress Display and Error Handling 520Error Handling in the flags2 Examples 525Using futures.as_completed 527Threading and Multiprocessing Alternatives 530Chapter Summary 530Further Reading 53118. Concurrency with asyncio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537Thread Versus Coroutine: A Comparison 539asyncio.Future: Nonblocking by Design 545Yielding from Futures, Tasks, and Coroutines 546Downloading with asyncio and aiohttp 548Running Circling Around Blocking Calls 552Enhancing the asyncio downloader Script 554Using asyncio.as_completed 555Using an Executor to Avoid Blocking the Event Loop 560From Callbacks to Futures and Coroutines 562Doing Multiple Requests for Each Download 564Writing asyncio Servers 567xii | Table of ContentsAn asyncio TCP Server 568An aiohttp Web Server 573Smarter Clients for Better Concurrency 576Chapter Summary 577Further Reading 579Part VI. Metaprogramming19. Dynamic Attributes and Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585Data Wrangling with Dynamic Attributes 586Exploring JSON-Like Data with Dynamic Attributes 588The Invalid Attribute Name Problem 591Flexible Object Creation with __new__ 592Restructuring the OSCON Feed with shelve 594Linked Record Retrieval with Properties 598Using a Property for Attribute Validation 604LineItem Take #1: Class for an Item in an Order 604LineItem Take #2: A Validating Property 605A Proper Look at Properties 606Properties Override Instance Attributes 608Property Documentation 610Coding a Property Factory 611Handling Attribute Deletion 614Essential Attributes and Functions for Attribute Handling 616Special Attributes that Affect Attribute Handling 616Built-In Functions for Attribute Handling 616Special Methods for Attribute Handling 617Chapter Summary 619Further Reading 61920. Attribute Descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Descriptor Example: Attribute Validation 625LineItem Take #3: A Simple Descriptor 626LineItem Take #4: Automatic Storage Attribute Names 631LineItem Take #5: A New Descriptor Type 637Overriding Versus Nonoverriding Descriptors 640Overriding Descriptor 642Overriding Descriptor Without __get__ 643Nonoverriding Descriptor 644Overwriting a Descriptor in the Class 645Methods Are Descriptors 646Table of Contents | xiiiDescriptor Usage Tips 648Descriptor docstring and Overriding Deletion 650Chapter Summary 651Further Reading 65121. Class Metaprogramming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655A Class Factory 656A Class Decorator for Customizing Descriptors 659What Happens When: Import Time Versus Runtime 661The Evaluation Time Exercises 662Metaclasses 101 666The Metaclass Evaluation Time Exercise 669A Metaclass for Customizing Descriptors 673The Metaclass __prepare__ Special Method 675Classes as Objects 677Chapter Summary 678Further Reading 679Afterword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683A. Support Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Python Jargon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复